package org.lee;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author lizhe
 */
public class TestJDBC {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/blur?useSSL=false&useUnicode=true&characterEncoding=utf8";
        Connection connection = DriverManager.getConnection(url, "root", "root");
        Statement statement = connection.createStatement();

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        int i = statement.executeUpdate("insert into student ( name, gender,course,addTime) values('小明','男','Chinese','" + dtf.format(now) + "')");
        System.out.println("入库数量：" + i);

        ResultSet resultSet = statement.executeQuery("select * from student limit 100");
        print(resultSet);

        int updateCount = statement.executeUpdate("update student set name='小李' where name='小明'");
        System.out.println("修改了" + updateCount + "条数据");

        ResultSet resultSet1 = statement.executeQuery("select * from student limit 100");
        print(resultSet1);

        int deleteCount = statement.executeUpdate("delete from student where name = '小李'");
        System.out.println("删除了" + deleteCount + "条数据");

        statement.close();
        connection.close();

    }

    public static void print(ResultSet resultSet) throws SQLException {
        while (resultSet.next()) {
            long id = resultSet.getLong("id");
            String name = resultSet.getString("name");
            String gender = resultSet.getString("gender");
            String course = resultSet.getString("course");
            String addTime = resultSet.getString("addTime");
            System.out.println(id + " " + name + " " + gender + " " + course + " " + addTime);
        }
    }
}
